package com.ruoyi.crm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.crm.domain.*;
import com.ruoyi.crm.service.Impl.*;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *
 * 公海管理控制层
 * @author  dxy
 *
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pool")
public class PoolCustomerController extends BaseController {
    private final PoolCustomerServiceImpl poolCustomerService;
    private final CustomerServiceImpl customerService;
    private final CustomerTypeServiceImpl customerTypeService;
    private final SourceInfoServiceImpl sourceInfoService;
    private final IndustryInfoServiceImpl industryInfoService;
    private final CompanySizeServiceImpl companySizeService;
    private final ResponsibleServiceImpl responsibleService;
    private final ContactServiceImpl contactService;
    private final CustomerTierServiceImpl customerTierService;
    private final CustomerTagsServiceImpl customerTagsService;
    private final DepartmentServiceImpl departmentService;

    /**
     * 查询公海客户
     */
    @SaCheckPermission("poolCustomer:poolCustomer:list")
    @GetMapping("/list")
    public TableDataInfo<PoolCustomer> selectPoolCustomerPageList(PoolCustomer poolCustomer, PageQuery pageQuery) {
        TableDataInfo<PoolCustomer> list = poolCustomerService.selectPoolCustomerPageList(poolCustomer,pageQuery);
        list.getRows().forEach(pool -> {
            if(pool.getCustomerId()!=null) {
                CustomerInfo customerInfo = customerService.selectCustomerById(pool.getCustomerId());
                if (customerInfo != null) {
                    pool.getParams().put("customerName", customerInfo.getCustomerName());
                    pool.getParams().put("website", customerInfo.getWebsite());
                }
            }
            if(pool.getCustomerTypeId()!=null) {
                CustomerType customerType = customerTypeService.selectCustomerTypeById(pool.getCustomerTypeId());
                if (customerType != null) {
                    pool.getParams().put("type", customerType.getType());
                }

            }
            if(pool.getSourceId()!=null) {
                SourceInfo sourceInfo=sourceInfoService.SelectSourceInfoById(pool.getSourceId());
                if (sourceInfo != null) {
                    pool.getParams().put("sourceName", sourceInfo.getSourceName());
                }
            }

            if (pool.getCustomerId()!=null) {
                IndustryInfo industryInfo=industryInfoService.selectIndustryInfoById(pool.getIndustryId());
                if (industryInfo != null) {
                    pool.getParams().put("industryName", industryInfo.getIndustryName());
                }
            }
            if (pool.getCustomerTypeId()!=null) {
                CompanySize companySize=companySizeService.selectCompanySizeById(pool.getCustomerTypeId());
                if (companySize != null) {
                    pool.getParams().put("companySizeName", companySize.getCompanySizeName());
                }
            }
            if (pool.getCompanySizeId()!=null) {
                CompanySize companySize=companySizeService.selectCompanySizeById(pool.getCompanySizeId());
                if (companySize != null) {
                    pool.getParams().put("companySizeName", companySize.getCompanySizeName());
                    pool.getParams().put("licenseNumber", companySize.getLicenseNumber());
                }
            }
            if (pool.getResponsiblePersonId()!=null) {
                Responsible responsible=responsibleService.selectResponsibleById(pool.getResponsiblePersonId());
                if (responsible != null) {
                    pool.getParams().put("responsiblePersonName", responsible.getResponsiblePersonName());
                }
            }
            if (pool.getContactId()!=null) {
                ContactInfo contactInfo=contactService.selectContactById(pool.getContactId());
                if (contactInfo != null) {
                    pool.getParams().put("contactName", contactInfo.getContactName());
                    pool.getParams().put("phone",contactInfo.getPhone());
                    pool.getParams().put("landlineNumber",contactInfo.getLandlineNumber());
                    pool.getParams().put("email",contactInfo.getEmail());
                    pool.getParams().put("address",contactInfo.getAddress());
                }
            }
            if(pool.getCustomerTierId()!=null) {
                CustomerTier customerTier=customerTierService.selectCustomerTierById(pool.getCustomerTierId());
                if (customerTier != null) {
                    pool.getParams().put("customerTierName", customerTier.getCustomerTierName());
                }
            }
            if (pool.getCustomerTagsId()!=null) {
                CustomerTags customerTags=customerTagsService.selectCustomerTagsById(pool.getCustomerTagsId());
                if (customerTags != null) {
                    pool.getParams().put("customerTagsName", customerTags.getCustomerTagsName());
                }
            }
            if(pool.getDepartmentId()!=null) {
                DepartmentInfo departmentInfo=departmentService.selectDepartmentById(pool.getDepartmentId());
                if (departmentInfo != null) {
                    pool.getParams().put("departmentName", departmentInfo.getDepartmentName());
                }
            }




        });
        return list;
    }
    /**
     * 修改公海客户
     */
    @SaCheckPermission("poolCustomer:poolCustomer:edit")
    @Log(title = "公海客户",businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody PoolCustomer poolCustomer) {
        return toAjax(poolCustomerService.updatePoolCustomerById(poolCustomer));

    }

    /**
     * 删除公海客户
     */
    @SaCheckPermission("poolCustomer:poolCustomer:remove")
    @Log(title = "公海客户",businessType = BusinessType.DELETE)
    @DeleteMapping("/{poolId}")
    public R<Void> deletePoolCustomer(@PathVariable Long poolId) {
        return toAjax(poolCustomerService.deletePoolCustomerById(poolId));
    }

    /**
     * 导出公海客户
     */
    @SaCheckPermission("poolCustomer:poolCustomer:export")
    @Log(title = "公海客户",businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PoolCustomer poolCustomer){
        List<PoolCustomer> list = poolCustomerService.selectPoolCustomerList(poolCustomer);
        List<PoolCustomer> listVo= BeanUtil.copyToList(list,PoolCustomer.class);
        ExcelUtil.exportExcel(list, "公海客户信息", PoolCustomer.class, response);

    }
    /**
     * 导入公海客户
     */
    @SaCheckPermission("poolCustomer:poolCustomer:import")
    @Log(title = "公海客户",businessType = BusinessType.IMPORT)
    @PostMapping(value = "/import",consumes =  MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        // 导入方法
        ExcelResult<PoolCustomer> result = ExcelUtil.importExcel(file.getInputStream(), PoolCustomer.class,false);
        List<PoolCustomer> list = result.getList();
        boolean imp=poolCustomerService.insertBatch(list);
        if(!imp){
            return R.fail("导入失败，保存数据库时出错");
        }
        return R.ok(result.getAnalysis());
    }




}
